<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i>
                    {{pageTitle}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <acc-set-card></acc-set-card>
        <div class="container" :style="{'min-height': containerHeight + 'px'}">
            <div class="handle-box flex flex-center">
                <el-button type="primary" icon="el-icon-folder" @click="saveEdit()">保存</el-button>
            </div>
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px">
                <el-form-item label="账套代码">
                    <el-input v-model="ruleForm.setBookId" disabled placeholder="后台自动生成"></el-input>
                </el-form-item>
                <el-form-item label="账套名称" prop="description">
                    <el-input v-model="ruleForm.description"></el-input>
                </el-form-item>
                <el-form-item label="账套状态" prop="status">
                    <el-select v-model="ruleForm.status" placeholder="请选择">
                        <el-option label="新增" :value="0"></el-option>
                        <el-option label="启用" :value="1" v-if="isUpdate"></el-option>
                        <el-option label="冻结" :value="2" v-if="isUpdate"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="会计期间" prop="period">
                    <el-input v-model="ruleForm.period" disabled></el-input>
                </el-form-item>
                <el-form-item label="当前会计期间" prop="beginPer" disabled>
                    <el-date-picker
                        v-model="ruleForm.beginPer"
                        type="month"
                        value-format="yyyyMM"
                        placeholder="选择会计期间"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="公司名称" prop="companyName">
                    <div @click="showCustomerDialog('客户')" v-if="!isUpdate">
                        <el-input v-model="ruleForm.companyName" readonly>
                            <i slot="suffix" class="el-icon-search"></i>
                        </el-input>
                    </div>
                    <div v-else>
                        <el-input v-model="ruleForm.companyName" disabled>
                            <i slot="suffix" class="el-icon-search"></i>
                        </el-input>
                    </div>
                </el-form-item>
                <el-form-item label="交易币别" prop="currency">
                    <el-input v-model="ruleForm.currency" disabled></el-input>
                </el-form-item>
                <el-form-item label="本位币精度" prop="accuracy">
                    <el-input v-model="ruleForm.accuracy" disabled></el-input>
                </el-form-item>
            </el-form>
        </div>

        <customer-search
            :isShow="showCustomerSearch"
            :dialogTitle="customerSearchDialogTit"
            method="clientInfoFindAllPageByDTO"
            @edit="customerSearchBack"
            @close="closeDialog"
        ></customer-search>
    </div>
</template>


<script>
import appMain from '../../../utils/app_main';
import _ from 'lodash';
import XEUtils from 'xe-utils';

export default {
    data() {
        return {
            pageTitle: '',
            containerHeight: '',
            //会计期间
            periodList: [],
            rules: {
                description: [
                    { required: true, message: '请输入账套名称', trigger: 'change' },
                    { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'change' }
                ],
                companyName: [
                    { required: true, message: '请输入公司名称', trigger: 'change' },
                    { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'change' }
                ],
                beginPer: [{ required: true, message: '请选择', trigger: 'change' }],
                rate: [{ required: true, message: '请输入', trigger: 'change' }]
            },
            rules1: {
                description: [{ min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'change' }]
            },
            ruleForm: {
                accuracy: 6, //本位币精度,小数位数 写死传6
                beginPer: '', //当前会计期间, 接口查询
                companyId: '', //公司ID 接口查询数据
                companyName: '', //公司名称  接口查询数据
                currency: 'CNY', //本位币代码，默认CNY  接口查询数据
                description: '', //账套名称
                period: '12', //会计期间,默认传12
                status: 0 //状态：0新增 1 启用 2冻结
            }, //新增model
            editForm: {},
            isUpdate: false,
            customerSearchDialogTit: '',
            showCustomerSearch: false
        };
    },
    created() {
        //this.getData();
        //如果有id过来就是修改的
        if (this.$route.query.id) {
            let id = this.$route.query.id;
            //是更新操作
            this.isUpdate = true;
            //查询账套详情
            //this.getDetailById(id);
            this.getAccSetById(id);
        } else {
            //新增操作
            this.isUpdate = false;
        }
    },
    mounted() {
        var div_height = window.screen.availHeight;
        var height = div_height - 100;
        this.containerHeight = height - 200;
        this.pageTitle = this.$router.currentRoute.meta.title;
        //$refs.xTree.setAllTreeExpand(true);
    },
    methods: {
        addRate() {
            appMain
                .setBooksInsert(this.ruleForm)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.$message.success(res.data.message);
                        this.$router.push({
                            path: '/accSetList' //跳转的路径
                        });
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        updateRate() {
            appMain
                .setBooksUpdateById(this.ruleForm)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.$message.success(res.data.message);
                        this.$router.push({
                            path: '/accSetList' //跳转的路径
                        });
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        getAccSetById(id) {
            appMain
                .setBooksFindById({}, id)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.ruleForm = res.data.data;
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        accPeriodGetEnums() {
            appMain
                .accPeriodGetEnums()
                .then(res => {
                    if (res.data.code === 20000) {
                        this.ruleForm.beginPer = res.data.data;
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        getPeriodList() {
            appMain
                .accPeriodFindDefaultData()
                .then(res => {
                    if (res.data.code === 20000) {
                        this.periodList = res.data.data;
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    if (formName == 'searchRuleForm') {
                        //新增科目
                        this.getData();
                    } else if (formName == 'ruleForm') {
                        this.addRate();
                    } else if (formName == 'editForm') {
                        this.updateRate();
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        saveEdit() {
            if (this.isUpdate == false) {
                this.submitForm('ruleForm');
            } else if (this.isUpdate == true) {
                this.submitForm('editForm');
            }
        },
        // 选择客户
        showCustomerDialog(tit) {
            this.customerSearchDialogTit = tit;
            this.showCustomerSearch = true;
        },
        // 客户搜索结果返回
        customerSearchBack(data) {
            console.log(data.backData, 'customerSearchBack');
            this.ruleForm.companyId = data.backData.id;
            this.ruleForm.companyName = data.backData.name;
            this.ruleForm.companyCode = data.backData.code;

            this.showCustomerSearch = false;
        },
        closeDialog(data) {
            this.showSearch = data.showDialog;
            this.showCustomerSearch = false;
        }
    },
    computed: {},
    watch: {}
};
</script>

<style lang="css" scoped>
.el-input {
    max-width: 360px;
}

.el-card {
    height: 100%;
}

.flex-right {
    padding: 20px;
}

.handler-box {
    border: 1px solid rgb(232, 234, 236);
    padding: 5px;
    margin-bottom: 15px;
}

.tree-box .flex-left {
    padding: 0px !important;
}

.handle-box {
    padding: 5px 50px;
    width: 100%;
    background-color: #fff;
    position: absolute;
    left: 0;
    bottom: 0px;
    border-bottom: 1px solid #ddd;
    border-top: 1px solid #ddd;
    z-index: 1;
}
</style>